package com.zzh.net.Interceptor;


import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;


public class LoggingInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl requestUrl = request.url();


        RequestBody requestBody = request.body();
        Buffer buffer = new Buffer();
        requestBody.writeTo(buffer);
        Charset charset = Charset.forName("UTF-8");
        MediaType contentType = requestBody.contentType();
        if (contentType != null) {
            charset = contentType.charset(charset);
        }
        String paramsStr = buffer.readString(charset);
        //打印发送信息
        Response response = chain.proceed(chain.request());
        ResponseBody responseBody = response.peekBody(1024 * 1024);//关键代码
        String json = responseBody.string();
//        Logger.i("requestUrl   :    " + requestUrl + "&" + paramsStr + "\n" + json);
        return response;
    }

}
